package org.travel.app.service.impl;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.travel.app.entity.IdVerification;
import org.travel.app.entity.spots.Spots;
import org.travel.app.entity.user.User;
import org.travel.app.entity.user.UserCenterMsg;
import org.travel.app.entity.user.UserSpotsStart;
import org.travel.app.mapper.UserMapper;
import org.travel.app.service.UserService;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * @author nmm
 * @date 2024-11-2024/11/22
 * @deriction njm......
 */
@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {
  private final UserMapper userMapper;
  private final RedisTemplate<String, Object> redisTemplate;

  @Override
  public List<User> getUserList() {
    return userMapper.selectAllUser();
  }


  /**
   * 返回用户个人信息
   *
   * @param user
   * @return
   */
  public User returnUser(User user) {
    User user1 = (userMapper.enSureUserExit(user.getUserName()) == null ? userMapper.enSurePhoneEXit(user.getPhone()) : userMapper.enSureUserExit(user.getUserName()));
    return user1;
  }

  @Override
  public Boolean enSureUserExit(User user) {
    Boolean enSure = false;
    String name = user.getUserName();
    String phone = user.getPhone();
    if (name != null) {
      enSure = enSureUserByName(user, enSure);
    }
    if (phone != null) {
      enSure = enSureUserByPhone(user, enSure);
    }
    return enSure;
  }

  /**
   * 通过用户名查询用户是否存在
   *
   * @param user
   * @param status
   * @return
   */
  @Override
  public Boolean enSureUserByName(User user, Boolean status) {
    User user1 = null;
    if (userMapper.enSureUserExit(user.getUserName()) != null) {
      //如果查询到用户
      user1 = userMapper.enSureUserExit(user.getUserName());
      if (user1.getPassword().equals(user.getPassword())) {
        //判断用户输入密码是否正确
        status = true;
      }
    }
    return status;
  }

  /**
   * 通过用户手机号查询用户是否存在
   *
   * @param user
   * @param status
   * @return
   */
  @Override
  public Boolean enSureUserByPhone(User user, Boolean status) {
    User user1 = null;
    if (userMapper.enSurePhoneEXit(user.getPhone()) != null) {
      //如果查询到用户
      user1 = userMapper.enSurePhoneEXit(user.getPhone());
      if (user1.getPassword().equals(user.getPassword())) {
        //判断用户输入密码是否正确
        status = true;
      }
    }
    return status;
  }

  @Override
  public void addUser(User user) {
    user.setUserName("user" + System.currentTimeMillis());
    user.setRegisterTime(LocalDateTime.now());
    userMapper.addUser(user);
  }


  @Override
  public void addReservationUser(IdVerification reservationUser) {
    userMapper.addReservationUser(reservationUser);
  }

  @Override
  public List<IdVerification> getAllReservationUser() {
    return userMapper.getAllReservationUser();
  }

  @Override
  public void enSureSomeUser(IdVerification idVerification) {
    userMapper.enSureSomeUser(idVerification);
  }

  @Override
  public Integer getPointsByUserId(int userId) {
    return userMapper.getPointsByUserId(userId);
  }

  @Override
  public Integer getUserOrderCount(int userId) {
    return userMapper.getUserOrderCount(userId);
  }

  @Override
  public Integer getUserTicketCount(int userId) {
    return userMapper.getUserTicketCount(userId);
  }

  @Override
  public Integer getUserStartCount(int userId) {
    return userMapper.getUserStartCount(userId);
  }


  /**
   * 用户收藏
   *
   * @param userSpotsStart
   */
  @Override
  public void addUserStart(UserSpotsStart userSpotsStart) {
    userMapper.addUserStart(userSpotsStart);
  }

  @Override
  public void updateUserStart(UserSpotsStart userSpotsStart) {
    userMapper.updateUserStart(userSpotsStart);
  }


  /**
   * 判断用户是否有当前收藏的商品(用于前端进行显示是否高亮收藏)
   * @param userSpotsStart
   * @return
   */
  @Override
  public UserSpotsStart isHaveStartSpots(UserSpotsStart userSpotsStart) {
     UserSpotsStart spotsStart=  userMapper.isHaveStartSpots(userSpotsStart);
     if (spotsStart!=null){
        userSpotsStart=spotsStart;
        //不为空就将当前状态给到收藏对象
     }else {
         userSpotsStart.setHasStart(false);
         //否则收藏状态设置为空
     }
     return userSpotsStart;
  }

  /**
   * 获取用户收藏景点
   * @param userId
   * @return
   */
  @Override
  public List<Spots> getStartSpots(int userId) {
    return userMapper.getStartSpots(userId);
  }


  /**
   * 检查用户是否重复收藏
   *
   * @param userSpotsStart
   */
  public void checkIsStart(UserSpotsStart userSpotsStart) {
    //获取集合中的所有元素
    Set<Object> userStart = redisTemplate.opsForSet().members("userStart");
    if (checkHasStartId(userStart,userSpotsStart)) {
        //如果已经有过收藏的记录
        //就直接通过前端的状态来操作数据库
        updateUserStart(userSpotsStart);
    }else {
        //否则就进行第一次收藏(true,因为默认是未收藏)
        userSpotsStart.setHasStart(true);
        userSpotsStart.setStartTime(new Timestamp(System.currentTimeMillis()));
        //设置当前收藏时间
        addUserStart(userSpotsStart);
        //当执行修改时不记录进缓存
        //只记录添加过的收藏id(用于判断是否是新添加的收藏对象)
      if (!checkHasStartId(userStart,userSpotsStart)){
        redisTemplate.opsForSet().add("userStart", userSpotsStart.getId());
      }
    }
  }


  /**
   * 判断是否是第一次添加
   * @param userStart
   * @param userSpotsStart
   * @return
   */
  public boolean checkHasStartId(Set<Object> userStart, UserSpotsStart userSpotsStart) {
    boolean isHasStartId=false;
    if (userStart != null) {
      for (Object startId : userStart) {
        if (startId == userSpotsStart.getId()) {
            isHasStartId=true;
        }
        //判断缓存里是否已经有收藏过的商品
      }
    }
    return  isHasStartId;
  }


  /**
   * 获取用户中心统计数量
   * @return
   */
  public UserCenterMsg getUserCountMsg(int userId) {
      return new UserCenterMsg(getPointsByUserId(userId),getUserOrderCount(userId),getUserTicketCount(userId),getUserStartCount(userId));
  }

}